package service

import (
	"teacher-helper/dao"
	"teacher-helper/models"
	"teacher-helper/models/request"
)

func AddOneGrade(Grade models.SysGrade) error {
	return dao.DB.Create(&Grade).Error
}


func GetGradeList(queryInfo request.QueryInfo) (Grades []models.SysGrade, total int, err error) {
	if queryInfo == (request.QueryInfo{}){
		err = dao.DB.Find(&Grades).Error
		total = len(Grades)
		return
	}
	if queryInfo.Query == "" {
		if err = dao.DB.Model(&models.SysGrade{}).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&Grades).Error
	} else {
		if err = dao.DB.Model(&models.SysGrade{}).Where("name like ?", "%"+queryInfo.Query+"%").Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where("name like ?", "%"+queryInfo.Query+"%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&Grades).Error
	}
	return
}

func UpdateGrade(Grade models.SysGrade) error {
	return dao.DB.Model(&models.SysGrade{}).Where(&models.SysGrade{ID:Grade.ID}).Update(&models.SysGrade{Name:Grade.Name}).Error
}

func DeleteGrade(id int) error {
	return dao.DB.Delete(&models.SysGrade{ID:id}).Error
}

